﻿Imports NewHeaderSystems.Archive.Core
Imports NewHeaderSystems.Archive.Data

Public Class AccountMaintenanceEditor


    Private _accountView As AccountView
    Public Property AccountView() As AccountView
        Get
            Return _accountView
        End Get
        Set(ByVal value As AccountView)
            _accountView = value
        End Set
    End Property
    Public Sub New(ByVal accountView As AccountView)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        DialogResult = Windows.Forms.DialogResult.Cancel 'Set default user might click close button on title
        ' Add any initialization after the InitializeComponent() call.
        _accountView = accountView
    End Sub

    Private Sub txtAccountID_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAccountID.Validating
        ValidateRequired(txtAccountID, "Please enter an Account ID.")
    End Sub

    Private Sub txtCustodianID_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCustodianID.Validating
        ValidateRequired(txtCustodianID, "Please enter a Custodian ID.")
    End Sub

    Private Sub ValidateRequired(ByVal textBox As TextBox, ByVal message As String)
        Dim isValid As Boolean = Not String.IsNullOrEmpty(textBox.Text) And textBox.Text.Trim().Length > 0
        If isValid Then
            ErrorProvider.SetError(textBox, String.Empty)
        Else
            ErrorProvider.SetError(textBox, message)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ValidForm() Then
            If DataScript.IsExistingCustodian(_accountView.ID, txtCustodianID.Text) Then
                MessageBox.Show("This Custodian ID already exists, please type in a different Custodian ID.", "Duplicate Custodian ID", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                txtCustodianID.Focus()
            Else
                DataScript.SaveAccount(_accountView.ID, txtAccountID.Text, txtCustodianID.Text, txtAccountName.Text)
                DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()
            End If
        End If
    End Sub

    Private Function ValidForm() As Boolean
        Return String.IsNullOrEmpty(ErrorProvider.GetError(txtAccountID)) And String.IsNullOrEmpty(ErrorProvider.GetError(txtCustodianID))
    End Function

    Private Sub AccountMaintenanceEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtAccountID.Text = _accountView.AccountID
        txtCustodianID.Text = _accountView.CustodianID
        txtAccountName.Text = _accountView.AccountName
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class